package ynu.beggar.server.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ynu.beggar.server.dao.SuggestionDao;
import ynu.beggar.server.dao.UserInfoDao;
import ynu.beggar.server.entities.DO.*;
import ynu.beggar.server.entities.PO.SuggestionPO;
import ynu.beggar.server.entities.PO.UserInfoPO;
import ynu.beggar.server.service.UserService;
import ynu.beggar.server.utils.CommonUtils;
import ynu.beggar.server.utils.JsonData;
import ynu.beggar.server.utils.JwtUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import static ynu.beggar.server.utils.JsonData.buildError;
import static ynu.beggar.server.utils.JsonData.buildSuccess;

@Service
public class UserServiceImpl implements UserService {

    private UserInfoDao userInfoDao;
    private SuggestionDao suggestionDao;

    @Autowired
    private void setUserInfoDao(UserInfoDao userInfoDao) {
        this.userInfoDao = userInfoDao;
    }

    @Autowired
    private void setSuggestionDao(SuggestionDao suggestionDao) {
        this.suggestionDao = suggestionDao;
    }


    @Override
    public JsonData login(UserLoginDO userLoginDO) {
        String pwMD5 = CommonUtils.MD5(userLoginDO.getPassword());
        List<UserInfoPO> idList = userInfoDao.findUserIdByStuIdAndUserPw(userLoginDO.getUserNameOrStuID(), pwMD5);
        if (idList.size() == 0) {
            idList = userInfoDao.findUserIdByUserNameAndUserPw(userLoginDO.getUserNameOrStuID(), pwMD5);
            if (idList.size() == 0) {
                return buildError("用户名、学号或密码错误");
            }
        }
//        根据userBasicInfoDO创建token
        String token = JwtUtils.geneJsonWebToken(new UserBasicInfoDO(idList.get(0)));
        return buildSuccess(new HashMap<String, String>() {{
            put("token", token);
        }});
    }

    @Override
    public JsonData register(UserRegisterDO userRegisterDO) {
        if (userExist(userRegisterDO)) {
            return buildError("用户名或学号已存在");
        } else {
//            存数据
            UserInfoPO userInfoPO = new UserInfoPO(userRegisterDO);
            userInfoDao.save(userInfoPO);
//            根据userBasicInfoDO创建token
            String token = JwtUtils.geneJsonWebToken(new UserBasicInfoDO(userInfoPO));
            return buildSuccess(new HashMap<String, String>() {{
                put("token", token);
            }});
        }
    }

    @Override
    public JsonData createSuggestion(SuggestionDO suggestionDO, UserBasicInfoDO userBasicInfoDO) {
        SuggestionPO suggestionPO = new SuggestionPO(suggestionDO, userBasicInfoDO);
        suggestionDao.save(suggestionPO);
        return JsonData.buildSuccess("发送建议成功");
    }

    @Override
    public JsonData changeInfo(UserChangeInfoDO userChangeInfoDO, UserBasicInfoDO userBasicInfoDO) {
        Optional<UserInfoPO> opt = userInfoDao.findById(userBasicInfoDO.getUserId());
        if (opt.isPresent()) {
            UserInfoPO userInfoPO = new UserInfoPO(userChangeInfoDO, opt.get());
            userInfoDao.save(userInfoPO);
            return JsonData.buildSuccess("修改信息成功");
        } else {
            return JsonData.buildError("请登录");
        }
    }

    @Override
    public JsonData getInfo(UserBasicInfoDO userBasicInfoDO) {
        Optional<UserInfoPO> opt = userInfoDao.findById(userBasicInfoDO.getUserId());
        if (opt.isPresent()) {
            UserChangeInfoDO userChangeInfoDO = new UserChangeInfoDO(opt.get());
            return JsonData.buildSuccess(userChangeInfoDO);
        } else {
            return JsonData.buildError("请登录");
        }
    }


    private boolean userExist(UserRegisterDO user) {
        return userInfoDao.existsByUserName(user.getRgName()) &&
                userInfoDao.existsByStuId(user.getRgStuId());
    }
}
